package org.batatasframework.chart;

import java.util.ArrayList;
import java.util.List;

import org.batatasframework.chart.columns.ChartColumnAxis;

public class ChartRow {
	
	private ChartCell axis;
	private final ChartCell[] cells;
	
	protected ChartRow(ChartCell[] cells){
		this.cells = cells;
	}
	
	public ChartCell[] getCells() {
		return cells;
	}
	
	public ChartRow read(ChartCell axis){
		this.axis = axis;
		return this;
	}
	
	public ChartRow read(ChartColumnAxis axis, Object[] row){
		this.axis = axis.newCell();
		this.axis.read(row);
		return this;
	}
	
	public ChartRow read(Object[] row){
		for(ChartCell c : cells){
			c.read(row);
		}
		return this;
	}
	
	/** 进行累计 */
	public ChartRow add(ChartRow augend){
		int len = cells.length;
		for(int i = 0; i < len; i++){
			cells[i].add(augend.cells[i]);
		}
		return this;
	}

	private final static int DEFAULT_ROW_SIZE = 20;
	
	public Object[] writeData() {
		List<Object> item = new ArrayList<>(DEFAULT_ROW_SIZE);
		for(ChartCell c : cells){
			c.write(item);
		}
		return item.toArray();
	}
	
	public Object[] writeRow() {
		List<Object> item = new ArrayList<>(DEFAULT_ROW_SIZE);
		axis.write(item);
		for(ChartCell c : cells){
			c.write(item);
		}
		return item.toArray();
	}
}
